<?php
$this->title = "导入excel";
?>
<div class="main">
	<div class="header">导入excel</div>
	<div class="oper">
		<a href="../web/excel/sample.xlsx" class='btn btn-primary'>导入Excel样例文件</a>
	</div>
	<?php if($msg !=''){
		if ($msg == '数据导入成功') {
	?>
	<div class="alert alert-success col-sm-10" role="alert"><?=$msg?></div>
	<?php }else{?>
	<div class="alert alert-danger col-sm-10" role="alert"><?=$msg?></div>
	<?php }}?>
	<form id="excel-form" class="form-horizontal" action="<?=ROOT_URL?>importdata/add" method="post" enctype="multipart/form-data" onsubmit="return subCheck();">
		<div id="excel-container">
		<div class="form-group">
			<label class="col-sm-3 control-label" style="text-align:center">Excel文件</label>
			<label class="col-sm-3 control-label" style="text-align:center">文件名</label>
			<label class="col-sm-3 control-label" style="text-align:center">描述</label>			
		</div>
		<div class="form-group">			
			<div class="col-sm-3">
				<input type="file" class="fileinput" name="excelfile">
			</div>			
			<div class="col-sm-3">
				<input type="text" class="form-control" name="excelfile_name">
			</div>			
			<div class="col-sm-3">
				<input type="text" class="form-control" name="excelfile_desc">
			</div>
			<div class="col-sm-1">
				<input id="addbtn" type="button" class="btn btn-primary btn-sm" value="添加">
			</div>
		</div>
		</div>
		<div class="form-group">
			<label class="col-sm-4 control-label"></label>
			<div class="col-sm-4">
				<input id="submitbtn" type="submit" class="btn btn-primary" value="确定上传" style="width:100px;">
			</div>
		</div>
	</form>
</div>
<script type="text/javascript">
var fileCount = 0;
$("#addbtn").click(function(){
	fileCount++;

	var str = "";
	str += "<div class='form-group'>";			
	str += "<div class='col-sm-3'><input type='file' class='fileinput' name='excelfile"+fileCount+"'></div>";			
	str += "<div class='col-sm-3'><input type='text' class='form-control' name='excelfile"+fileCount+"_name'></div>";			
	str += "<div class='col-sm-3'><input type='text' class='form-control' name='excelfile"+fileCount+"_desc'></div>";
	str += "<div class='col-sm-1'><input type='button' class='delbtn btn btn-danger btn-sm' value='删除'></div>";
	str += "</div>";	
	$("#excel-container").append(str);
});

$(document).on("click",".delbtn",function(){
	if ($(this).parents("div.form-group").find(".fileinput").val() != "") {
		if(confirm("确定删除该文件吗")){
			$(this).parents("div.form-group").remove();
		}
	}
	else{
		$(this).parents("div.form-group").remove();
	}
	
});

function subCheck()
{
	var flag = false;
	var fileElements = $(".fileinput");
	for(var i=0;i<fileElements.length;i++)
	{
		if($(fileElements[i]).val() != "")
		{
			flag = true;
		}
	}

	if (!flag) {
		alert("请至少上传一个Excel文件");
	}
	return flag;
}
</script>


